//
//  LZLoginViewController.m
//  ThorWebViewSDK_Example
//
//  Created by panfei mao on 2019/1/16.
//  Copyright © 2019 panfei mao. All rights reserved.
//

#import "LZLoginViewController.h"
#import <ThorWebViewSDK/ThorFramework.h>

#define kChannelName @"YiCYX_geren"

@interface LZLoginViewController ()

@property (nonatomic, strong) UITextField   *channel;
@property (nonatomic, strong) UIButton      *choiseChannel;

@property (nonatomic, strong) UITextField   *sign;

@property (nonatomic, strong) UITextField   *userId;
@property (nonatomic, strong) UIButton      *choiseId;

@property (nonatomic, strong) UITextField   *userPhone;
@property (nonatomic, strong) UITextField   *userToken;
@property (nonatomic, strong) UIButton      *choisePhone;

@end

@implementation LZLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"登陆";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismiss)];
    
    CGFloat viewWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat textFieldX = 20.0;
    CGFloat textFieldW = viewWidth - 40.0 - 80.0;
    CGFloat textFieldH = 44.0;
    CGFloat textFeildY = 120.0;
    
    UITextField *channelTextField = [[UITextField alloc] init];
    channelTextField.placeholder = @"渠道名称";
    channelTextField.frame = CGRectMake(textFieldX, textFeildY, textFieldW, textFieldH);
    channelTextField.borderStyle = UITextBorderStyleLine;
    channelTextField.text = kChannelName;
    channelTextField.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:channelTextField];
    self.channel = channelTextField;
    
    UILabel *channel = [[UILabel alloc] init];
    channel.text = @"渠道名:";
    channel.font = [UIFont systemFontOfSize:14];
    channel.frame = CGRectMake(CGRectGetMinX(self.channel.frame), CGRectGetMinY(self.channel.frame) - 30.0, 200.0, 30);
    [self.view addSubview:channel];
    
    self.choiseChannel = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.choiseChannel setTitle:@"选择渠道" forState:UIControlStateNormal];
    self.choiseChannel.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.choiseChannel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.choiseChannel addTarget:self action:@selector(choiseChannelAction) forControlEvents:UIControlEventTouchUpInside];
    self.choiseChannel.frame = CGRectMake(CGRectGetMaxX(self.channel.frame) + 10.0, CGRectGetMinY(self.channel.frame) , 80.0, CGRectGetHeight(self.channel.frame));
    self.choiseChannel.layer.cornerRadius = 4.0;
    self.choiseChannel.clipsToBounds = YES;
    self.choiseChannel.backgroundColor = [UIColor orangeColor];
//    [self.view addSubview:self.choiseChannel];
    
    UITextField *userIdTextField = [[UITextField alloc] init];
    userIdTextField.placeholder = @"用户ID";
    userIdTextField.frame = CGRectMake(textFieldX, CGRectGetMaxY(self.channel.frame) + 40.0, textFieldW, textFieldH);
    userIdTextField.borderStyle = UITextBorderStyleLine;
    userIdTextField.clearButtonMode = UITextFieldViewModeAlways;
    userIdTextField.text = @"2173013";
    [self.view addSubview:userIdTextField];
    self.userId = userIdTextField;
    
    self.choiseId = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.choiseId setTitle:@"选择用户ID" forState:UIControlStateNormal];
    self.choiseId.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.choiseId setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.choiseId addTarget:self action:@selector(choiseIdAction) forControlEvents:UIControlEventTouchUpInside];
    self.choiseId.frame = CGRectMake(CGRectGetMaxX(self.userId.frame) + 10.0, CGRectGetMinY(self.userId.frame) , 80.0, CGRectGetHeight(self.userId.frame));
    self.choiseId.layer.cornerRadius = 4.0;
    self.choiseId.clipsToBounds = YES;
    self.choiseId.backgroundColor = [UIColor orangeColor];
    //    [self.view addSubview:self.choiseId];
    
    UILabel *userId = [[UILabel alloc] init];
    userId.text = @"用户ID:";
    userId.font = [UIFont systemFontOfSize:14];
    userId.frame = CGRectMake(CGRectGetMinX(self.userId.frame), CGRectGetMinY(self.userId.frame) - 30.0, 200.0, 30);
    [self.view addSubview:userId];
    
    
    UITextField *userPhoneTextField = [[UITextField alloc] init];
    userPhoneTextField.placeholder = @"用户手机号";
    userPhoneTextField.frame = CGRectMake(textFieldX, CGRectGetMaxY(self.userId.frame) + 40.0, textFieldW, textFieldH);
    userPhoneTextField.borderStyle = UITextBorderStyleLine;
    userPhoneTextField.clearButtonMode = UITextFieldViewModeAlways;
    userPhoneTextField.text = @"15268189338";
    [self.view addSubview:userPhoneTextField];
    self.userPhone = userPhoneTextField;
    
    self.choisePhone = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.choisePhone setTitle:@"选择手机号" forState:UIControlStateNormal];
    self.choisePhone.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.choisePhone setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.choisePhone addTarget:self action:@selector(choisePhoneAction) forControlEvents:UIControlEventTouchUpInside];
    self.choisePhone.frame = CGRectMake(CGRectGetMaxX(self.userPhone.frame) + 10.0, CGRectGetMinY(self.userPhone.frame) , 80.0, CGRectGetHeight(self.userPhone.frame));
    self.choisePhone.layer.cornerRadius = 4.0;
    self.choisePhone.clipsToBounds = YES;
    self.choisePhone.backgroundColor = [UIColor orangeColor];
    //    [self.view addSubview:self.choisePhone];
    
    
    UILabel *userPhone = [[UILabel alloc] init];
    userPhone.text = @"用户手机号:";
    userPhone.font = [UIFont systemFontOfSize:14];
    userPhone.frame = CGRectMake(CGRectGetMinX(self.userPhone.frame), CGRectGetMinY(self.userPhone.frame) - 30.0, 200.0, 30);
    [self.view addSubview:userPhone];
    
    UITextField *userTokenTextField = [[UITextField alloc] init];
    userTokenTextField.placeholder = @"用户Token";
    userTokenTextField.frame = CGRectMake(textFieldX, CGRectGetMaxY(self.userPhone.frame) + 40.0, textFieldW, textFieldH);
    userTokenTextField.borderStyle = UITextBorderStyleLine;
    userTokenTextField.clearButtonMode = UITextFieldViewModeAlways;
    userTokenTextField.text = @"yyToken";
//    [self.view addSubview:userTokenTextField];
    self.userToken = userTokenTextField;
    
    UILabel *userToken = [[UILabel alloc] init];
    userToken.text = @"用户token:";
    userToken.font = [UIFont systemFontOfSize:14];
    userToken.frame = CGRectMake(CGRectGetMinX(self.userToken.frame), CGRectGetMinY(self.userToken.frame) - 30.0, 200.0, 30);
//    [self.view addSubview:userToken];
    
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitButton setTitle:@"登   陆" forState:UIControlStateNormal];
    [submitButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitButtonAction) forControlEvents:UIControlEventTouchUpInside];
    submitButton.frame = CGRectMake(40.0, CGRectGetMaxY(self.userToken.frame) + 40.0, viewWidth - 80.0, 44.0);
    submitButton.layer.cornerRadius = 22.0;
    submitButton.clipsToBounds = YES;
    submitButton.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:submitButton];
}

//  登陆
- (void)submitButtonAction{
    [self.view endEditing:YES];
    
    NSString *userId    = self.userId.text;
    NSString *userPhone = self.userPhone.text;
    NSString *channel   = self.channel.text;
    
    NSString *userInfo = [NSString stringWithFormat:@"当前用户信息\n用 户 名:         %@\n用 户 ID：      %@\n用户手机号：%@\n渠   道   名：  %@",@"杨阳", userId, userPhone, channel];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(loginWithUserInfo:)]) {
        [self.delegate loginWithUserInfo:userInfo];
    }
    
    [[ThorBaseConfig sharedConfig] loginWithUserPhone:userPhone andUserId:userId andChannel:channel];
    [self dismissViewControllerAnimated:YES completion:nil];
}

//  选择渠道
- (void)choiseChannelAction{
    
}

//  选择用户ID
- (void)choiseIdAction{

}

//  选择用户手机号
- (void)choisePhoneAction{
}

//  退出
- (void)dismiss{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
